package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.ZodiacResultAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.ZodiacResultBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.CommentDecoration
import kotlinx.android.synthetic.main.activity_zodiac_result.*
import kotlinx.android.synthetic.main.view_network_error.*

class ZodiacResultActivity : BaseActivity() {


    private var mAdapter: ZodiacResultAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_zodiac_result)
        initView()
        request()
        activityShowAd("Interstitial_Zodiac")
    }

    private fun initView() {
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            ""
        )

        mRecyclerView = findViewById(R.id.recycler_view_zodiac_result)
        mAdapter = ZodiacResultAdapter(this)
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        mRecyclerView.addItemDecoration(CommentDecoration(15f, 20f))
        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            request()
        }
    }

    private fun request() {
        val h1 = intent?.getStringExtra(YOU)
        val h2 = intent?.getStringExtra(PARTNER)
        if (h1 == null || h2 == null) {
            include_error.visibility = View.VISIBLE
        } else {
            showLoading()
            NetworkEngine.getZodiacCompatibility(h1, h2, listener)
        }
    }

    private val listener = object : NetworkListener<ZodiacResultBean>() {
        override fun onSuccess(t: ZodiacResultBean) {
            mAdapter?.setHeadBean(t.data)
            mAdapter?.setData(t.data.content)
            showLoaded()
            include_error.visibility = View.GONE
        }

        override fun onFailed(t: ZodiacResultBean) {
            super.onFailed(t)
            errorMake()
        }

        override fun onError(e: String?) {
            super.onError(e)
            errorMake()
        }

    }

    private fun errorMake() {
        showLoaded()
        include_error.visibility = View.VISIBLE
    }

    companion object {
        private const val YOU = "you"
        private const val PARTNER = "partner"
        fun generateIntent(context: Context, youName: String, partner: String): Intent {
            val intent = Intent(context, ZodiacResultActivity::class.java)
            intent.putExtra(YOU, youName)
            intent.putExtra(PARTNER, partner)
            return intent
        }
    }
}